package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.tribes.util.UUIDGenerator;

import pojo.Order;
import service.ShopManager;
import service.impl.ShopManagerFactory;
import utils.BookMap;
import utils.UUIDGererator;

/**
 * Servlet implementation class PayServlet
 */
public class PayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ShopManager manager;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PayServlet() {
        super();
        manager = ShopManagerFactory.getInstance();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Order> orderList = new ArrayList<Order>();
		Map<String, Integer> bookMap = BookMap.getBookMap();
		for(Entry<String, Integer> entry:bookMap.entrySet()){    
			Order o = new Order();
			o.setBid(entry.getKey());
			o.setCount(entry.getValue());
			o.setOid(UUIDGererator.getUUID());
			o.setoDate(new Date());
			orderList.add(o);
			manager.addOrder(o);
		}
		bookMap.clear();
		request.setAttribute("orderList", orderList);
		request.getRequestDispatcher("orderList.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
